leaderboardApp.controller("progresosCtrl", function($scope, $http, $timeout, $location, notifier, httpPost) {
	$scope.loading = false;
	$scope.message = {title: "", text: "", type: "", visible: false};
	$scope.modalMessage = {title: "", text: "", type: "", visible: false};
	
	$scope.progresosUsuario = [];
	$scope.progresosSucursal = [];
	$scope.progresosGeneral = [];
	
	$scope.ventasMeta = [];
	$scope.currentProgreso = {};
	$scope.currentUsrName = "";
	$scope.currentSucursal = "";
	
	$scope.branches = [];
	$scope.branch = {id: 0, name: ""};
	
	$scope.avgProgresoMetasUsr = 0;
	$scope.sumPonderacionMetasUsr = 0;
	$scope.avgNotasUsr = 0;
	$scope.avgProgresoUsersSuc = 0;
	$scope.sumPonderacionUsersSuc = 0;
	
	$scope.showDetalleUsuario = false;
	$scope.showDetalleSucursal = false;
	
	$scope.mesesProgresos = [];
	$scope.mesSeleccionado = {};
	
	$scope.supervisores = [];
	$scope.supervisor = {};
	
	$scope.getMesesProgresos = function() {
		$scope.loading = true;
		httpPost.send($scope.message, "/Leaderboard/ProgresosServlet/getMesesProgresos", {}, function(data) {
			if(data.code === 0) {
				$scope.mesesProgresos= data.meses;
				if($scope.mesesProgresos.length > 0) {
					$scope.mesSeleccionado = $scope.mesesProgresos[$scope.mesesProgresos.length-1];
				}
				if($("#cmbMeses").select2 != null) {
					$("#cmbMeses").select2();
					$timeout(function() { $("#cmbMeses").trigger("change"); }, 0, false);
				}
				$scope.getProgresosCurrentUsuario(); 
			}
			$scope.loading = false;
		});
	};
	
	$scope.getSupervisores = function() {
		$scope.loading = true;
		httpPost.send($scope.message, "/Leaderboard/AdminServlet/getSupervisores", {"showAllSelector": "YES"}, function(data) {
			if(data.code === 0) {
				$scope.supervisores = data.supervisores;
				if($scope.supervisores.length > 0) {
					$scope.supervisor = $scope.supervisores[$scope.supervisores.length - 1]; 
				}
				if($("#cmbSupervisores").select2 != null) {
					$("#cmbSupervisores").select2();
					$timeout(function() { $("#cmbSupervisores").trigger("change"); }, 0, false);
				}
			}
		});
		$scope.loading = false;
	};
	
	$scope.getProgresosCurrentUsuario = function() {
		httpPost.send($scope.message, "/Leaderboard/ProgresosServlet/getProgresosUsuario", {"anio": $scope.mesSeleccionado.year, "mes": $scope.mesSeleccionado.month}, function(data) {
			if(data.code === 0) {
				$scope.progresosUsuario = data.progresos;
				$scope.avgProgresoMetasUsr = data.promedioProgresos;
				$scope.sumPonderacionMetasUsr = data.totalPonderacion;
				$scope.avgNotasUsr = data.promedioNotas;
			}
		});
	};
	
	$scope.getProgresosUsuario = function(userId) {
		httpPost.send($scope.message, "/Leaderboard/ProgresosServlet/getProgresosUsuario", {"userId": userId, "anio": $scope.mesSeleccionado.year, "mes": $scope.mesSeleccionado.month}, function(data) {
			if(data.code === 0) {
				$scope.progresosUsuario = data.progresos;
				$scope.avgProgresoMetasUsr = data.promedioProgresos;
				$scope.sumPonderacionMetasUsr = data.totalPonderacion;
				$scope.avgNotasUsr = data.promedioNotas;
			}
		});
	};
	
	$scope.getDetalleProgreso = function(progreso) {
		$scope.currentProgreso = progreso;
		httpPost.send($scope.message, "/Leaderboard/ProgresosServlet/getDetalleProgreso", {"progresoId": progreso.id}, function(data) {
			if(data.code === 0) {
				$scope.ventasMeta = data.ventasMeta;
				$("#dlgDetalleProgresoMeta").modal("show");
			}
		});
	};
	
	$scope.getDetalleProgresoUsr = function(usrId, usrName) {
		$scope.getProgresosUsuario(usrId);
		$scope.currentUsrName = usrName;
		$scope.showDetalleUsuario = true;
		$scope.showDetalleSucursal = false;
	};
	
	$scope.getDetalleProgresoSucursal = function(branchId, branchName) {
		$scope.branch.id = branchId;
		$scope.currentSucursal = branchName;
		$scope.getProgresosSucursal();
		$scope.showDetalleSucursal = true;
	};
	
	$scope.backToSucursales = function() {
		$scope.showDetalleUsuario = false;
		$scope.showDetalleSucursal = true;
	};
	
	$scope.backToGenerales = function() {
		$scope.showDetalleSucursal = false;
		$scope.showDetalleUsuario = false;
	};
	
	$scope.getProgresosSucursal = function() {
		var postParams = {};
		if($scope.branch.id > 0) {
			postParams["branchId"] = $scope.branch.id;
		}
		postParams["anio"] = $scope.mesSeleccionado.year;
		postParams["mes"] = $scope.mesSeleccionado.month;
		
		$scope.loading = true;
		if($scope.branch.id > 0) {
			httpPost.send($scope.message, "/Leaderboard/ProgresosServlet/getProgresosSucursal", postParams, function(data) {
				if(data.code === 0) {
					$scope.progresosSucursal = data.progresos;
					$scope.avgProgresoUsersSuc = data.promedioProgresos;
					$scope.sumPonderacionUsersSuc = data.totalPonderacion;
				}
				$scope.loading = false;
			});
		}
	};
	
	$scope.getProgresosGeneral = function() {
		$scope.loading = true;
		var params = {};
		if($scope.supervisor != null) {
			if($scope.supervisor.id > 0) {
				params["supervisorId"] = $scope.supervisor.id;
			}
		}
		httpPost.send($scope.message, "/Leaderboard/ProgresosServlet/getProgresosGeneral", params, function(data) {
			if(data.code === 0) {
				$scope.progresosGeneral = data.progresos;
			}
			$scope.loading = false;
		});
	};
	
	$scope.getBranches = function() {
		httpPost.send($scope.message, "/Leaderboard/AdminServlet/getBranchesUser", {"showOwnedOnly": "S"}, function(data) {
			if(data.code === 0) {
				$scope.branches = data.branches;
				if($scope.branches.length > 0) {
					$scope.branch = $scope.branches[0];
					$scope.getProgresosSucursal();
				}
				if($("#cmbBranches").select2 != null) {
					$("#cmbBranches").select2();
					$timeout(function() { $("#cmbBranches").trigger("change"); }, 0, false);
				}
			}
		});
	};
	
	$scope.setCurrentMes = function(mes) {
		$scope.mesSeleccionado = mes;
	};
	
	$scope.classForMes = function(mes) {
		if(mes.year === $scope.mesSeleccionado.year && mes.month === $scope.mesSeleccionado.month) {
			return "active";
		} else {
			return "";
		}
	};
	
	$scope.openImportDialog = function() {
		$("#dlgImportPgrs").modal("show");
	};
	
	/************************************** Funciones de Utiler�a **************************************/
	
	$scope.hideMessage = function() {
		notifier.hideNotif($scope.message);
	};
	
	/************************************** ##################### **************************************/
});